<template>
	<div class="evidence-borrow-edit-container">
		<el-card shadow="hover" body-style="padding:8px; ">
			<div class="card-header" style="width: 100%; margin-top: 5px; border-top: 1px solid AliceBlue">
				<el-row justify="end" style="margin-bottom: 15px; margin-top: 3px">
					<el-col :span="8">
						<div style="float: left; height: 40px; width: 5px; background: green"></div>
						<div style="float: left; margin-top: 10px; margin-left: 20px; font-size: 16px">案件信息</div>
					</el-col>
					<el-col :span="16" style="text-align: right"> </el-col>
				</el-row>
				<ShowCase :formInline="formInline" />
			</div>
			<div class="card-header" style="width: 100%; margin-top: 5px; border-top: 1px solid AliceBlue">
				<el-row justify="end" style="margin-bottom: 15px; margin-top: 3px">
					<el-col :span="8">
						<div style="float: left; height: 40px; width: 5px; background: green"></div>
						<div style="float: left; margin-top: 10px; margin-left: 20px; font-size: 16px">案卷列表</div>
					</el-col>
					<el-col :span="16" style="text-align: right">
						<!-- <el-button type="primary" icon="el-icon-add" @click="onEvidenceInfo" size="mini">选择案卷</el-button> -->
					</el-col>
				</el-row>
				<EveidenceList ref="refEveidenceList" :showOperation="true" :outType="outType" :caseId="formInline.id" />
			</div>
			<div class="card-header" style="width: 100%; margin-top: 5px; border-top: 1px solid AliceBlue">
				<el-row justify="end" style="margin-bottom: 15px; margin-top: 3px">
					<el-col :span="8">
						<div style="float: left; height: 40px; width: 5px; background: green"></div>
						<div style="float: left; margin-top: 10px; margin-left: 20px; font-size: 16px">{{ outTypeTitle }}信息</div>
					</el-col>
					<el-col :span="16" style="text-align: right"> </el-col>
				</el-row>
				<EditComp ref="refEditComp" :refForm="refForm" :outType="outType" />
			</div>
			<el-row justify="center" style="margin-top: 10px">
				<el-button type="primary" icon="el-icon-edit" @click="onSubmit" align="center" size="medium">{{ outTypeTitle }}</el-button>
			</el-row>
			<div style="color:red;">请对文书、报告、情况说明等凭证进行拍照或上传操作</div>
		</el-card>
		<ChooseEvi ref="chooseEviRef" />
	</div>
</template>
<script lang="ts">
import { ref, reactive, toRefs, getCurrentInstance } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import ShowCase from '/@/views/evidence/unit/component/showCase.vue';
import ChooseEvi from '/@/views/evidence/common/chooseEvi.vue';
import EveidenceList from '/@/views/evidence/common/evidenceList.vue';
import EditComp from '/@/views/evidence/common/editComp.vue';
import { useStore } from '/@/store/index';
import { postEvidenceTransfere } from '/@/api/evidence/transfere';
import { ElMessage } from 'element-plus';
import { getOutType } from '/@/common/index';
import { Session } from '/@/utils/storage';
export default {
	name: 'BorrowEdit',
	components: { ShowCase, EveidenceList, EditComp, ChooseEvi },
	activated(){
		this.formInline = this.$store.state.customs.customs.caseInfo;
		var self = this;
		setTimeout(function(){
			self.$refs.refEveidenceList.init();
		},100);
		
	},
	setup() {
		const { proxy } = getCurrentInstance() as any;
		const router = useRouter();
		const route = useRoute();
		const chooseEviRef = ref();
		const store = useStore();
		const refEveidenceList = ref();
		const refEditComp = ref();
		const refForm=ref();
		const userInfo = Session.get('userInfo');
		const state = reactive({
			outTypeTitle: getOutType(route.query.outType),
			outType: Number(route.query.outType),
			formInline: store.state.customs.customs.caseInfo,
		});
		const onEvidenceInfo = () => {
			chooseEviRef.value.openDialog();
		};
		const onSubmit = async () => {
			const _tableData = refEveidenceList.value.tableData;
			const _formInline = refEditComp.value.formInline;
			_formInline.transferorOrgan = userInfo.organName;
			_formInline.transferor = userInfo.userName;
			_formInline.transferorPhone = userInfo.phone;
			let _evidenceStatus = 2;
			if (state.outType == 6 || state.outType == 8) {
				_evidenceStatus = 1;
			}
			if (state.outType == 6) {
				
				if (!refEditComp.value.formInline.locationId) {
					ElMessage.warning('请选择位置');
					return false;
				}
			}

			// if(_formInline.imgs.length==0)
			// {
			// 	ElMessage.warning('请上传或者拍照');
			// 	return;
			// }
			// debugger

			await refEditComp.value.refForm.validate((valid) => {
				if (valid) {
			
			// {
			// 	if (!refEditComp.value.formInline.remark) {
			// 		ElMessage.warning('请选择或者输入意见');
			// 		return false;
			// 	}
			// }
			const list = _tableData.map((x: any) => {
				const _data = {
					evidenceId: x.id,
					deliveryNo: '',
					evidenceStatus: _evidenceStatus,
					approveStatus: 0,
					deliveryDicType: state.outType,
					deliveryDicTypeName: getOutType(state.outType),
					EvidenceQuantity: x.quantity,
					location: _formInline.location,
					locationId: _formInline.locationId,
					returnTime: _formInline.returnTime,
					locationStatus: 0,
					receiver: _formInline.receiver,
					receiverOrgan: _formInline.receiverOrgan,
					receiverPhone: _formInline.receiverPhone,
					remark: _formInline.remark,
					transferorOrgan: '',
					transferor: '',
					transferorPhone: '',
				};
				if (state.outType == 6) {
					_data.transferorOrgan = _formInline.receiverOrgan;
					_data.transferor = _formInline.receiver;
					_data.transferorPhone = _formInline.receiverPhone;
					_data.receiverOrgan = '';
					_data.receiver = '';
					_data.receiverPhone = '';
				}
				return _data;
			});
			 postEvidenceTransfere({ imgs: _formInline.imgs, list: list, status: _evidenceStatus }).then((res: any) => {
				if (res.success) {
					ElMessage.success('操作成功');
					const eroute = router.getRoutes().find((x) => x.name == 'EvidenceCommonDetail') as any;
					eroute.meta.title = state.outTypeTitle + '确认';
					_formInline.deliveryTime = res.data.deliveryTime;
					store.dispatch('customs/setCustoms', {
						selectionEvidences: _tableData,
						formInline: state.formInline,
						transfereInfo: _formInline,
						deliveryNo: res.data.deliveryNo,
						signImg: res.data.signImg,
					});
					router.getRoutes().find((x) => x.name == 'EvidenceCommonDetail').title = state.outTypeTitle + '确认';
					router.getRoutes().find((x) => x.name == 'EvidenceCommonDetail').meta.title = state.outTypeTitle + '确认';
					router.push({ path: '/evidence/common/detail', query: { outType: route.query.outType } });
					closeCurrentTagsView();
				}
			});

		}
			});
		};
		const closeCurrentTagsView = () => {
			proxy.mittBus.emit('onCurrentContextmenuClick', Object.assign({}, { contextMenuClickId: 1, ...route }));
		};
		return {
			chooseEviRef,
			onEvidenceInfo,
			onSubmit,
			refEditComp,
			refForm,
			refEveidenceList,
			closeCurrentTagsView,
			state,
			...toRefs(state),
		};
	},
};
</script>
